package com.lsfb.signln.Statistics.EvectionSearch.SearchResult.PreviewPdf;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;
import com.github.barteksc.pdfviewer.listener.OnLoadCompleteListener;
import com.github.barteksc.pdfviewer.listener.OnPageChangeListener;
import com.lsfb.signln.R;
import com.lsfb.signln.Utils.PopWindowManager;
import com.umeng.socialize.ShareAction;
import com.umeng.socialize.UMShareAPI;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;
import com.umeng.socialize.media.UMWeb;
import com.zgscwjm.lsfbbasetemplate.LsfbActivity;
import com.zgscwjm.lsfbbasetemplate.annotation.ContentView;
import com.zgscwjm.lsfbbasetemplate.annotation.OnClick;
import com.zgscwjm.lsfbbasetemplate.annotation.ViewInject;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;


/**
 * pdf预览页
 * Created by Administrator on 2018/1/31 0031.
 */
@ContentView(R.layout.aty_preview_pdf)
public class PreviewPdfActivity extends LsfbActivity implements OnPageChangeListener, OnLoadCompleteListener {

    /**
     * pdf预览
     */
    @ViewInject(R.id.pdfView)
    private PDFView pdfView;

    /**
     * PopWindow管理类
     */
    private PopWindowManager replay;
    private String downloadUrl;
    UMWeb web;

    @Override
    public void initView() {
//        initImagePath(this);

        downloadUrl = getIntent().getStringExtra("downloadUrl");

        web = new UMWeb(downloadUrl);
        web.setTitle("个人差旅统计");//标题
        web.setDescription("下载个人差旅统计,pdf格式");//描述

        File file = new File(Environment.getExternalStorageDirectory() + File.separator + "sign" + File.separator + "个人出差统计.pdf");

        pdfView.fromFile(file)
                .pages(0) // all pages are displayed by default
                .enableSwipe(true)
                .swipeHorizontal(false)
                .enableDoubletap(true)
                .defaultPage(0)
                //.onDraw(onDrawListener)
                .onLoad(PreviewPdfActivity.this)
                .onPageChange(this)
                //.onPageScroll(onPageScrollListener)
                //.onError(onErrorListener)
                .enableAnnotationRendering(false)
                .password(null)
                .scrollHandle(null)
                .load();
    }

    @Override
    public void initEvent() {

    }

    /**
     * 点击事件
     *
     * @param view
     */
    @OnClick({R.id.aty_back, R.id.aty_share})
    public void onClick(View view) {
        switch (view.getId()) {
            //返回
            case R.id.aty_back:
                finish();
                break;
            //分享
            case R.id.aty_share:
                replay = new PopWindowManager(this, R.layout.pop_share) {
                    @Override
                    public void onViewLister(View contentView, final PopWindowManager manager) {

                        final RelativeLayout RelativeLayout = (RelativeLayout) contentView.findViewById(R.id.RelativeLayout);
                        final LinearLayout pop_share_wechat = (LinearLayout) contentView.findViewById(R.id.pop_share_wechat);
                        final LinearLayout pop_share_qq = (LinearLayout) contentView.findViewById(R.id.pop_share_qq);

                        RelativeLayout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                manager.hideWindow();
                            }
                        });
                        //微信分享
                        pop_share_wechat.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                new ShareAction(PreviewPdfActivity.this)
                                        .setPlatform(SHARE_MEDIA.WEIXIN)//传入平台
                                        .withMedia(web)
                                        .setCallback(umShareListener)//回调监听器
                                        .share();
                                manager.hideWindow();
                            }
                        });

                        //QQ分享
                        pop_share_qq.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                new ShareAction(PreviewPdfActivity.this)
                                        .setPlatform(SHARE_MEDIA.QQ)//传入平台
                                        .withMedia(web)
                                        .setCallback(umShareListener)//回调监听器
                                        .share();
                                manager.hideWindow();
                            }
                        });
                    }
                };
                replay.showWindows(getWindow().getDecorView());
                break;
        }
    }

    private UMShareListener umShareListener = new UMShareListener() {
        /**
         * @descrption 分享开始的回调
         * @param platform 平台类型
         */
        @Override
        public void onStart(SHARE_MEDIA platform) {

        }

        /**
         * @descrption 分享成功的回调
         * @param platform 平台类型
         */
        @Override
        public void onResult(SHARE_MEDIA platform) {
            Toast.makeText(PreviewPdfActivity.this, "分享成功", Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享失败的回调
         * @param platform 平台类型
         * @param t 错误原因
         */
        @Override
        public void onError(SHARE_MEDIA platform, Throwable t) {
            Toast.makeText(PreviewPdfActivity.this, "分享失败" + t.getMessage(), Toast.LENGTH_LONG).show();
        }

        /**
         * @descrption 分享取消的回调
         * @param platform 平台类型
         */
        @Override
        public void onCancel(SHARE_MEDIA platform) {
            Toast.makeText(PreviewPdfActivity.this, "取消分享", Toast.LENGTH_LONG).show();

        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }


    @Override
    public void initData() {

    }

    @Override
    public void loadComplete(int nbPages) {

    }

    @Override
    public void onPageChanged(int page, int pageCount) {

    }

    /**
     * 把图片从drawable复制到sdcard中
     *
     * @param mContext
     */
    public void initImagePath(Context mContext) {
        InputStream is = null;
        FileOutputStream fos = null;
        File file;
        try {
            String path = android.os.Environment.getExternalStorageDirectory()
                    .getPath();
            path = path + "/Fridom";

            String dbPathAndName = path + "/" + "file.pdf";

            file = new File(path);

            if (file.exists() == false) {

                file.mkdir();
            }

            File dbFile = new File(dbPathAndName);
            if (!dbFile.exists()) {
                // TODO: 2017/12/21 0021 这里报错无需管它
                is = mContext.getResources().openRawResource(+R.raw.file);
//                is = mContext.getResources().openRawResource(R.raw.icon_app);
                fos = new FileOutputStream(dbFile);

                byte[] buffer = new byte[8 * 1024];// 8K
                while (is.read(buffer) > 0)// >
                {
                    fos.write(buffer);
                }
            }

        } catch (Exception e) {

        } finally {
            try {
                if (is != null) {
                    is.close();
                }

                if (fos != null) {
                    fos.close();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();

            }
        }
    }
}
